package com.javastudy.liuhouse.tcs;
import javax.swing.*;
import java.awt.*;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.util.ArrayList;
import java.util.List;
import java.util.Random;
public class BrickBreakerGame extends JPanel implements KeyListener, Runnable {
    private static final int WIDTH = 600; // 窗口宽度
    private static final int HEIGHT = 400; // 窗口高度
    private static final int PADDLE_WIDTH = 100; // 挡板宽度
    private static final int PADDLE_HEIGHT = 10; // 挡板高度
    private static final int BALL_SIZE = 20; // 球的大小
    private static final int BRICK_WIDTH = 50; // 砖块宽度
    private static final int BRICK_HEIGHT = 20; // 砖块高度
    private static final int DELAY = 10; // 游戏刷新速度

    private int paddleX = WIDTH / 2 - PADDLE_WIDTH / 2; // 挡板初始位置
    private int ballX = WIDTH / 2 - BALL_SIZE / 2; // 球初始位置
    private int ballY = HEIGHT - 100; // 球初始位置
    private int ballSpeedX = 3; // 球水平速度
    private int ballSpeedY = -3; // 球垂直速度
    private boolean isRunning = true; // 游戏是否运行
    private List<Rectangle> bricks; // 砖块列表

    public BrickBreakerGame() {
        setPreferredSize(new Dimension(WIDTH, HEIGHT));
        setBackground(Color.BLACK);
        setFocusable(true);
        addKeyListener(this);

        initializeBricks();
    }

    // 初始化砖块
    private void initializeBricks() {
        bricks = new ArrayList<>();
        Random random = new Random();
        for (int i = 0; i < 5; i++) {
            for (int j = 0; j < 10; j++) {
                int brickX = j * (BRICK_WIDTH + 5) + 30;
                int brickY = i * (BRICK_HEIGHT + 5) + 30;
                bricks.add(new Rectangle(brickX, brickY, BRICK_WIDTH, BRICK_HEIGHT));
            }
        }
    }

    // 移动挡板
    private void movePaddle(int direction) {
        if (direction == KeyEvent.VK_LEFT && paddleX > 0) {
            paddleX -= 70;
        } else if (direction == KeyEvent.VK_RIGHT && paddleX < WIDTH - PADDLE_WIDTH) {
            paddleX += 70;
        }
    }

    // 移动球
    private void moveBall() {
        ballX += ballSpeedX;
        ballY += ballSpeedY;

        // 球撞到左右墙壁
        if (ballX <= 0 || ballX >= WIDTH - BALL_SIZE) {
            ballSpeedX = -ballSpeedX;
        }

        // 球撞到顶部
        if (ballY <= 0) {
            ballSpeedY = -ballSpeedY;
        }

        // 球撞到挡板
        if (ballY >= HEIGHT - PADDLE_HEIGHT - BALL_SIZE && ballX >= paddleX && ballX <= paddleX + PADDLE_WIDTH) {
            ballSpeedY = -ballSpeedY;
        }

        // 球撞到砖块
        for (Rectangle brick : new ArrayList<>(bricks)) {
            if (brick.intersects(new Rectangle(ballX, ballY, BALL_SIZE, BALL_SIZE))) {
                bricks.remove(brick);
                ballSpeedY = -ballSpeedY;
                break;
            }
        }

        // 球掉出底部
        if (ballY >= HEIGHT) {
            isRunning = false;
        }
    }

    // 绘制游戏界面
    @Override
    public void paintComponent(Graphics g) {
        super.paintComponent(g);
        draw(g);
    }

    // 绘制挡板、球和砖块
    private void draw(Graphics g) {
        if (isRunning) {
            // 绘制挡板
            g.setColor(Color.GREEN);
            g.fillRect(paddleX, HEIGHT - PADDLE_HEIGHT, PADDLE_WIDTH, PADDLE_HEIGHT);

            // 绘制球
            g.setColor(Color.RED);
            g.fillOval(ballX, ballY, BALL_SIZE, BALL_SIZE);

            // 绘制砖块
            g.setColor(Color.BLUE);
            for (Rectangle brick : bricks) {
                g.fillRect(brick.x, brick.y, brick.width, brick.height);
            }

            // 绘制分数
            g.setColor(Color.WHITE);
            g.setFont(new Font("Arial", Font.BOLD, 20));
            g.drawString("Bricks Left: " + bricks.size(), 10, 20);
        } else {
            gameOver(g);
        }
    }

    // 游戏结束
    private void gameOver(Graphics g) {
        g.setColor(Color.RED);
        g.setFont(new Font("Arial", Font.BOLD, 40));
        g.drawString("Game Over", WIDTH / 2 - 100, HEIGHT / 2);

        g.setColor(Color.WHITE);
        g.setFont(new Font("Arial", Font.BOLD, 20));
        g.drawString("Bricks Left: " + bricks.size(), WIDTH / 2 - 80, HEIGHT / 2 + 40);
    }

    // 游戏循环
    @Override
    public void run() {
        while (isRunning) {
            moveBall();
            repaint();
            try {
                Thread.sleep(DELAY);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    }

    // 键盘事件处理
    @Override
    public void keyPressed(KeyEvent e) {
        movePaddle(e.getKeyCode());
    }

    @Override
    public void keyReleased(KeyEvent e) {}

    @Override
    public void keyTyped(KeyEvent e) {}

    // 启动游戏
    public static void main(String[] args) {
        JFrame frame = new JFrame("打砖块游戏");
        BrickBreakerGame game = new BrickBreakerGame();
        frame.add(game);
        frame.pack();
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setVisible(true);

        new Thread(game).start();
    }
}
